package com.blockmeta.bbs.immodule.adapter;

import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.Postcard;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.pojo.InforPOJO;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.immodule.R;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.util.List;

public class personalHomePostAdapter extends BaseAdapter<InforPOJO.ListBean, BaseViewHolder> implements View.OnClickListener{
    public personalHomePostAdapter(int layoutResId, @Nullable List<InforPOJO.ListBean> data) {
        super(layoutResId, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, InforPOJO.ListBean item) {

        TextView tv_inv = helper.getView(R.id.invest_money);
        tv_inv.setVisibility(View.VISIBLE);
        String dollar = null;
        try {
            dollar = item.getExtra().getAmount().replace("美元", "");

        } catch (Exception e) {
            if (item.getExtra() == null) {
                return;
            }
            dollar = item.getExtra().getAmount();
        }
        tv_inv.setText("$" + dollar);

        if (!TextUtils.isEmpty(item.getImage())) {
            Glide.with(mContext).load(item.getImage()).placeholder(R.drawable.yoona).into((ImageView) helper.getView(R.id.item_news_type2_img));
        }
        helper.setText(R.id.item_news_type2_title, item.getTitle());
        CircleImageView cirImg = helper.getView(R.id.item_news_type2_from);
        if (!TextUtils.isEmpty(item.getAuthor_info().getAvatar())) {
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(cirImg);
        }

        try {
            String dateToStamp = FormatUtil.dateToStampHMS(item.getPost_date_format());
            helper.setText(R.id.item_news_type2_time, TimeUtil.InforMationTime(Integer.parseInt(dateToStamp)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.item_news_type2_views, item.getViews());
        helper.setText(R.id.item_news_type2_reply, item.getReplies());
        ToggleButton toggleButton = helper.getView(R.id.tg_type2_selector);
        toggleButton.setVisibility(View.GONE);

        helper.setText(R.id.item_news_type2_title, item.getTitle());
        LinearLayout main = helper.getView(R.id.item_news_type2_main);
        main.setTag(item);
        main.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        InforPOJO.ListBean item = (InforPOJO.ListBean) v.getTag();
        if (!TextUtils.isEmpty(item.getPost_name())) {//内站数据
            Postcard postcard = RouterHelper.buildPath(RouterPath.NEWS_INNER_DETAIL_ACTIVITY);
            postcard.withString(Constant.ONE, String.valueOf(item.getId()));
            postcard.withString(Constant.INFO_USERNAME, item.getAuthor_info().getName());
            postcard.navigation(mContext);
        } else {//外站
//            Intent intent = new Intent(mContext, NewsDetailOutActivity.class);
//            intent.putExtra(Constant.INFO_ID, item.getId() + "");
//            if (item.getImages() != null && item.getImages().size() != 0) {
//                intent.putExtra(Constant.INFO_THUMB, item.getImages().get(0));
//            }
//            mContext.startActivity(intent);

            RouterHelper.startActivity(mContext,RouterPath.NEWS_DETAIL_OUT_ACTIVITY
                    ,item.getId()+"");
        }
    }
}
